package com.km.metric;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.util.List;

public class MetricBuilder {

	public static String currentMetric() {
		StringBuffer result = new StringBuffer();
		result.append("#").append(MetricFactory.getInstance().getServiceNmae()).append("|")
				.append(MetricFactory.getInstance().getPort()).append(System.lineSeparator());
		MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
		result.append("java_heap_bytes{type=\"committed\",project=\"")
				.append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
				.append(MetricFactory.getInstance().getPort()).append("\"} ").append(heapMemoryUsage.getCommitted())
				.append(System.lineSeparator());
		result.append("java_heap_bytes{type=\"init\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(heapMemoryUsage.getInit()).append(System.lineSeparator());
		result.append("java_heap_bytes{type=\"used\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(heapMemoryUsage.getUsed()).append(System.lineSeparator());
		result.append("java_heap_bytes{type=\"max\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(heapMemoryUsage.getMax()).append(System.lineSeparator());
		MemoryUsage noHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
		result.append("java_nonheap_bytes{type=\"committed\",project=\"")
				.append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
				.append(MetricFactory.getInstance().getPort()).append("\"} ").append(noHeapMemoryUsage.getCommitted())
				.append(System.lineSeparator());
		result.append("java_nonheap_bytes{type=\"init\",project=\"")
				.append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
				.append(MetricFactory.getInstance().getPort()).append("\"} ").append(noHeapMemoryUsage.getInit())
				.append(System.lineSeparator());
		result.append("java_nonheap_bytes{type=\"used\",project=\"")
				.append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
				.append(MetricFactory.getInstance().getPort()).append("\"} ").append(noHeapMemoryUsage.getUsed())
				.append(System.lineSeparator());
		result.append("java_nonheap_bytes{type=\"max\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(noHeapMemoryUsage.getMax()).append(System.lineSeparator());
		ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
		result.append("java_threads{type=\"peak\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(threadMxBean.getPeakThreadCount()).append(System.lineSeparator());
		result.append("java_threads{type=\"daemon\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(threadMxBean.getDaemonThreadCount()).append(System.lineSeparator());
		result.append("java_threads{type=\"started\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(threadMxBean.getTotalStartedThreadCount()).append(System.lineSeparator());
		result.append("java_threads{type=\"current\",project=\"").append(MetricFactory.getInstance().getServiceNmae())
				.append("\",port=\"").append(MetricFactory.getInstance().getPort()).append("\"} ")
				.append(threadMxBean.getThreadCount()).append(System.lineSeparator());
		List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
		for (GarbageCollectorMXBean gcMxBean : gcMxBeans) {
			result.append("java_gc_ps_count" + "{type=\"").append(gcMxBean.getName().toLowerCase().replace(" ", "_"))
					.append("\",project=\"").append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
					.append(MetricFactory.getInstance().getPort()).append("\"} ").append(gcMxBean.getCollectionCount())
					.append(System.lineSeparator());
			result.append("java_gc_ps_time" + "{type=\"").append(gcMxBean.getName().toLowerCase().replace(" ", "_"))
					.append("\",project=\"").append(MetricFactory.getInstance().getServiceNmae()).append("\",port=\"")
					.append(MetricFactory.getInstance().getPort()).append("\"} ").append(gcMxBean.getCollectionTime())
					.append(System.lineSeparator());
		}
		return result.toString();
	}
	
}
